package com.yueyang.sojourn.server.controller.wx;

import com.github.pagehelper.PageInfo;
import com.yueyang.sojourn.server.common.repeatable.NotRepeatable;
import com.yueyang.sojourn.server.controller.BaseController;
import com.yueyang.sojourn.server.model.po.Share;
import com.yueyang.sojourn.server.model.vo.ShareVo;
import com.yueyang.sojourn.server.model.vo.SojournOrderVo;
import com.yueyang.sojourn.server.service.share.ShareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "(小程序)分享 API")
@RequestMapping("/api/wx/share")
public class ApiShareController extends BaseController {
    @Autowired
    private ShareService shareService;
    @Value("${qiniu.domain}")
    private String qiniuCdn;

    @PostMapping("")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "X-Token", paramType = "header", required = true),
            @ApiImplicitParam(name="merchId",value="商品id",dataType="long", paramType = "query",example="123456"),
            @ApiImplicitParam(name="type",value="分销类型 0:消息卡片 1:图片分享",dataType="long", paramType = "query", example = "1")
    })
    @ApiOperation(value = "获取分享二维码图片（目前是mock数据）", notes = "获取分享二维码图片，扫码进入小程序后会带一个scene参数。 scene取值示例:shareId=100086  shareId即分享id。")
    @NotRepeatable
    public ShareVo share(@RequestParam("merchId") Long merchId) {
        Share share = this.shareService.share(merchId, getLoginUserId());
        ShareVo shareResultVo = new ShareVo();
        BeanUtils.copyProperties(share, shareResultVo);
        shareResultVo.setShareImgUrl(qiniuCdn + "/" + share.getShareImg());
        return shareResultVo;
    }

    @GetMapping("/list")
    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "获取分享列表", notes = "获取分享列表")
    public PageInfo<ShareVo> myShareList(@RequestParam("pageNum") Integer pageNum,
                                         @RequestParam("pageSize") Integer pageSize) {
        return this.shareService.findUserShareList(getLoginUserId(), pageNum, pageSize);
    }

    @GetMapping("/orderList")
    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @ApiOperation(value = "获取分销订单列表", notes = "获取分销订单列表")
    public PageInfo<SojournOrderVo> shareOrderList(@RequestParam("pageNum") Integer pageNum,
                                                   @RequestParam("pageSize") Integer pageSize) {

        return this.shareService.findMyShareOrderList(getLoginUserId(), pageNum, pageSize);
    }
}
